package redis

import (
	"context"
	"fmt"
	"shop_srv/shop"
	"strconv"
)

//	func CartAdd(UserId, ShopId, Num int64, Title, Cover, Status string, Price float64) error {
//		key := fmt.Sprintf("cart:%d:%d", UserId, ShopId)
//		return Client.HMSet(context.Background(), key, map[string]interface{}{
//			"UserId": UserId,
//			"ShopId": ShopId,
//			"Num":    Num,
//			"Title":  Title,
//			"Cover":  Cover,
//			"Status": Status,
//			"Price":  Price,
//		}).Err()
//	}
func CartAdd(UserId, ShopId, Num int64, Title, Cover, Status string, price float64) error {
	key := fmt.Sprintf("cart:%d:%d", UserId, ShopId)
	return Client.HMSet(context.Background(), key, map[string]interface{}{
		"UserId": UserId,
		"ShopId": ShopId,
		"Num":    Num,
		"Title":  Title,
		"Cover":  Cover,
		"Status": Status,
		"Price":  price,
	}).Err()
}
func ListCart(UserId int64) (cartList []*shop.CartList, err error) {
	key := fmt.Sprintf("cart:%d:*", UserId)
	keys := Client.Keys(context.Background(), key).Val()
	for _, s := range keys {
		lists := Client.HGetAll(context.Background(), s).Val()
		title := lists["Title"]
		cover := lists["Cover"]
		status := lists["Status"]
		stock, _ := strconv.Atoi(lists["Stock"])
		price, _ := strconv.ParseFloat(lists["Price"], 64)
		userId, _ := strconv.Atoi(lists["UserId"])
		cartList = append(cartList, &shop.CartList{
			Title:  title,
			Cover:  cover,
			Status: status,
			Stock:  int64(stock),
			Price:  float32(price),
			UserId: int64(userId),
		})
	}
	return
}

//func ListCart(UserId int64) (cartList []*shop.CartList, err error) {
//	key := fmt.Sprintf("cart:%d:*", UserId)
//	keys := Client.Keys(context.Background(), key).Val()
//	for _, s := range keys {
//		lists := Client.HGetAll(context.Background(), s).Val()
//		title := lists["Title"]
//		cover := lists["Cover"]
//		status := lists["Status"]
//		stock, _ := strconv.Atoi(lists["Stock"])
//		price, _ := strconv.ParseFloat(lists["Price"], 64)
//		userId, _ := strconv.Atoi(lists["UserId"])
//		cartList = append(cartList, &shop.CartList{
//			Title:  title,
//			Cover:  cover,
//			Status: status,
//			Stock:  int64(stock),
//			Price:  float32(price),
//			UserId: int64(userId),
//		})
//	}
//	return
//}
